/**
 * 
 */
package com.shine.icis.blo.imp;

import com.shine.cotrun.exp.Export;
import com.shine.cotrun.exp.ExportConf;
import com.shine.cotrun.exp.ExportFactory;
import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.icis.blo.IOfConfirmProcService;
import com.shine.icis.dao.IETLContextVarDAO;
import com.shine.icis.dao.IOfConfirmProcDAO;
import com.shine.icis.dto.StatAmounts;
import com.shine.icis.hbean.ETLContextVar;
import com.shine.icis.hbean.OfConfirmProc;

/**
 * 
 * @author zsy
 *
 */
public class OfConfirmProcService implements IOfConfirmProcService {
	private IOfConfirmProcDAO ofConfirmProcDAO;
	private IETLContextVarDAO etlContextVarDAO;

	public void setOfConfirmProcDAO(IOfConfirmProcDAO ofConfirmProcDAO) {
		this.ofConfirmProcDAO = ofConfirmProcDAO;
	}
	public void setEtlContextVarDAO(IETLContextVarDAO etlContextVarDAO) {
		this.etlContextVarDAO = etlContextVarDAO;
	}

	@Override
	public OfConfirmProc getOfConfirmProcById(int id) {
		OfConfirmProc proc = ofConfirmProcDAO.findById(id);
		if (proc != null) {
			proc.setDetails(ofConfirmProcDAO.findDetail(id));
		}
		return proc;
	}

	@Override
	public ListPage<OfConfirmProc> getOfConfirmProcPage(QueryParameters qp) {
		if (qp == null) {
			return null;
		}
		return ofConfirmProcDAO.findListPage(qp);
	}

	@Override
	public String csExpOfConfirmProc(QueryParameters qp, String dir, String expType) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		//初始化配置信息
		ExportConf cfg = new ExportConf();
		cfg.setExpToDir(dir);
		cfg.setExpFileName("开放式基金分红");
		cfg.addField("交易日期", "formatOrderDate", String.class, 10);
		cfg.addField("确认日期", "formatConfirmDate", String.class, 10);
		cfg.addField("交收日期", "formatSettleDate", String.class, 10);
		cfg.addField("应收应付类型", "arapTypeText", String.class, 40);
		cfg.addField("TA代码", "oppPar", String.class, 20);
		cfg.addField("市场代码", "mktCodeText", String.class, 25);
		cfg.addField("业务类型", "arapBusiTypeText", String.class, 30);
		cfg.addField("币种", "curTypeText", String.class, 15);
		cfg.addField("金额", "arapAmt", Double.class);
		Export<OfConfirmProc> exp = ExportFactory.createInstance(expType, cfg);
		try {
			exp.init();//初始化工作
			
			//添加额处查询条件
			qp.setPageNo(1);
			qp.setPageSize(10000);
			
			ListPage<OfConfirmProc> page = ofConfirmProcDAO.findListPage(qp);//查第一页
			//第一页写到文件
			exp.export(page.getDataList());
			if (page.getTotalPageCount() > 1) {
				for (int i = 2; i <= page.getTotalPageCount(); i++) {
					qp.setPageNo(i);
					page = ofConfirmProcDAO.findListPage(qp);//查第i页
					//第i页写到文件
					exp.export(page.getDataList());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//结束写入
			exp.end();
		}
		return exp.getFileName();//返回文件名（含扩展名）
	}
	
	@Override
	public StatAmounts getStatAmount() {
		StatAmounts amts = new StatAmounts();
		ETLContextVar var = etlContextVarDAO.findByName("BUSI_DATE");
		int busiDate = Integer.parseInt(var.getVarValue());
		double amt = ofConfirmProcDAO.findAmount(busiDate, OfConfirmProc.ARAP_TYPE_A0);
		amts.setActSetAmt(amt);
		amt = ofConfirmProcDAO.findAmount(busiDate, OfConfirmProc.ARAP_TYPE_A1);
		amts.setShoSetAmt(amt);
		
		double amt03 = ofConfirmProcDAO.findAmount(busiDate, OfConfirmProc.ARAP_TYPE_03);
		double amt02 = ofConfirmProcDAO.findAmount(busiDate, OfConfirmProc.ARAP_TYPE_02);
		amts.setAccPayAmt(amt03 - amt02);
		return amts;
	}
	
	@Override
	public void addOfConfirmProc(Integer orderDate, Integer confirmDate, int settleDate, String arapType,
			String oppPar, String mktCode, String arapBusiType, String curType, double arapAmt) {
		OfConfirmProc cfm = new OfConfirmProc();
		cfm.setOrderDate(orderDate);
		cfm.setConfirmDate(confirmDate);
		cfm.setSettleDate(settleDate);
		cfm.setArapType(arapType);
		cfm.setOppPar(oppPar);
		cfm.setMktCode(mktCode);
		cfm.setArapBusiType(arapBusiType);
		cfm.setCurType(curType);
		cfm.setArapAmt(arapAmt);
		ETLContextVar var = etlContextVarDAO.findByName("BUSI_DATE");
		int busiDate = Integer.parseInt(var.getVarValue());
		cfm.setBusiDate(busiDate);
		ofConfirmProcDAO.save(cfm);
	}
	
	@Override
	public void modifyOfConfirmProc(int id, String arapType) {
		OfConfirmProc ocp = ofConfirmProcDAO.findById(id);
		if (ocp == null) {
			throw new IllegalArgumentException();
		}
		ocp.setArapType(arapType);
		ofConfirmProcDAO.update(ocp);
	}
	
	@Override
	public void deleteOfConfirmProc(int id) {
		ofConfirmProcDAO.deleteById(id);
	}
	
	@Override
	public void txGenOfConfirmProcData(boolean init) {
		String procSql = init ? "{call EB_STD.P_ICIS_OF_CONFIRM_INIT(?,?,?,?,?,?,?)}" : "{call EB_ANALYSE.P_ICIS_OF_CONFIRM_PROC(?,?,?,?,?,?,?)}";
		
		ETLContextVar var = etlContextVarDAO.findByName("BUSI_DATE");
		int busiDate = Integer.parseInt(var.getVarValue());
		var = etlContextVarDAO.findByName("INCREMENT");
		String increment = var.getVarValue();
		ofConfirmProcDAO.callProc(procSql, busiDate, increment);
	}
}
